﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data.SqlClient;
using System.IO;
using System.Reflection;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;


namespace VOEISHubPushServiceDatabase
{
    [RunInstaller(true)]
    public partial class DatabaseInstaller : System.Configuration.Install.Installer
    {
        public DatabaseInstaller()
        {
            InitializeComponent();
        }

    private string GetSql(string Name)
        {
            try
            {
                //Gets the current assembly.
                Assembly Asm = Assembly.GetExecutingAssembly();

                //Resources are named using a fully qualified name.
                Stream strm = Asm.GetManifestResourceStream(Asm.GetName().Name + "." + Name);

                //Reads the contents of the embedded file.
                StreamReader reader = new StreamReader(strm);

                return reader.ReadToEnd();
            }
            catch (Exception ex)
            {
                //Log(ex.ToString());
                throw ex;
            }
        }

        private void ExecuteSql(string serverName, string dbName, string Sql)
        {
            string connStr = "Data Source=" + serverName.ToString() + ";Initial Catalog=" + dbName + ";Integrated Security=SSPI;";
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                try
                {
                    Server server = new Server(new ServerConnection(conn));
                    server.ConnectionContext.ExecuteNonQuery(Sql);
                }
                catch (Exception ex)
                {
                    //Reports any errors and abort.
                    //Log(ex.ToString());
                    throw ex;
                }
            }
        }

        protected void AddDBTable(string serverName)
        {
            try
            {
                //Creates the database and installs the tables.
                string strScript = GetSql("sql.txt");
                ExecuteSql(serverName, "master", strScript);
            }
            catch (Exception ex)
            {
                //Reports and errors and abort.
                //Log(ex.ToString());
                throw ex;
            }
        }

        public override void Install(IDictionary stateSaver)
        {
            base.Install(stateSaver);
            //Log("Setup Started");
            string server = this.Context.Parameters["servername"];
            server = server.Replace("\\\\", "\\");
            AddDBTable(server);
        }

        //Commented out after testing.
        //public void Log(string str)
        //{
        //    StreamWriter Tex;
        //    try
        //    {
        //        Tex = File.AppendText(this.logFilePath);
        //        Tex.WriteLine(DateTime.Now.ToString() + " " + str);
        //        Tex.Close();
        //    }
        //    catch
        //    {}
        //}
    }
}
